package project;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.Random;


public class KafkaProducer {
    public static void main(String[] args) {
        // 1. 创建 kafka 生产者的配置对象
        Properties properties = new Properties();
        // 2. 给 kafka 配置对象添加配置信息：bootstrap.servers
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "niit01:9092");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        // 每隔5秒推送 5条数据
        while (true) {
            // 3. 创建 kafka 生产者对象
            org.apache.kafka.clients.producer.KafkaProducer<String, String> kafkaProducer = new org.apache.kafka.clients.producer.KafkaProducer<String, String>(properties);
            System.out.println("开始发送数据");
            try {
                // 4. 调用 send 方法,发送消息
                for (int i = 0; i < 5; i++) {
                    Random rd=new Random();  //创建实例
                    // 品类
                    String[] studentArray = new String[]{
                            "201443\tjack1\t1\t20221211\t14982488207\t1\tL",
                            "201443\tJesson1\t0\t20211128\t15032910503\t1\tA",
                            "202018\tMerry1\t0\t20221211\t15236789912\t2\tL",
                            "202018\tJack2\t1\t20220912\t13782560338\t2\tA",
                            "202019\tSteve\t1\t20220807\t15908303289\t3\tL",
                            "202019\tLily3\t0\t20221102\t13745902335\t3\tA",
                            "202777\tMike\t0\t20220911\t13745902111\t4\tL",
                            "202777\tRobert\t1\t20220807\t15908093289\t4\tA",
                            "202011\tLily0\t1\t20221102\t13745902335\t5\tL",
                            "202011\tMike9\t0\t20220911\t13711982111\t5\tA",
                            "202012\tLily10\t1\t20221102\t13777702335\t6\tL",
                            "202012\tLucky\t0\t20220911\t13745666211\t6\tA",
                            "202016\tMike123\t0\t20210807\t15908403289\t7\tL",
                            "202016\tLily17\t1\t20221202\t13712302335\t7\tA",
                            "202346\tSusan11\t1\t20220930\t13712302111\t8\tL",
                            "202346\tMike11\t0\t20220913\t13712300987\t8\tA",
                            "201443\tjack1\t1\t20221211\t14982548207\t1\tA",
                            "201443\tJesson1\t0\t20211128\t15073291503\t1\tL",
                            "202018\tMerry1\t0\t20221211\t15236789912\t2\tA",
                            "202018\tJack2\t1\t20220912\t13782560338\t2\tL",
                            "202019\tSteve\t1\t20220807\t15908903289\t3\tA",
                            "202019\tLily3\t0\t20221102\t13745902335\t3\tL",
                            "202777\tMike\t0\t20220911\t13745902111\t4\tA",
                            "202777\tRobert\t1\t20220807\t15908032890\t4\tL",
                            "202011\tLily0\t1\t20221102\t13745902335\t5\tA",
                            "202011\tMike9\t0\t20220911\t13711988821\t5\tL",
                            "202012\tLily10\t1\t20221102\t13777702335\t6\tA",
                            "202012\tLucky\t0\t20220911\t13745666211\t6\tL",
                            "202016\tMike123\t0\t20210807\t15908003289\t7\tA",
                            "202016\tLily17\t1\t20221202\t13712302335\t7\tL",
                            "202346\tSusan11\t1\t20220930\t13712302111\t8\tA",
                            "202346\tMike11\t0\t20220913\t13712300987\t8\tL"
                    };
                    int radom =rd.nextInt(32);
                    String student  = studentArray[radom];
                    // 模拟数据
                    System.out.println("发送数据为" + student);

                    try {
                        kafkaProducer.send(new ProducerRecord<String, String>("stuInfo", student));

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                Thread.sleep(5000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            // 5. 关闭资源
            kafkaProducer.close();

        }

    }
}
